package com.mchsdk.paysdk.webview;

import com.mchsdk.paysdk.utils.MCHInflaterUtils;
import com.mchsdk.paysdk.utils.MCLog;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MCPayWebView extends Activity {

	private static final String TAG = "MCPayWebView";

	private WebView mWebView;
	private String param = "";
	private boolean isShowTopView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(MCHInflaterUtils.getLayout(this, "webview_mch_zfbpay"));

		param = getIntent().getStringExtra("zfbpay");
		isShowTopView = getIntent().getBooleanExtra("isshowtop", true);
		MCLog.e(TAG, "param = " + param);
		initView();
		initWebView();

	}

	@SuppressLint("SetJavaScriptEnabled")
	private void initWebView() {
		mWebView = (WebView) findViewById(MCHInflaterUtils.getControl(this, "wv_mch_pay"));
		mWebView.getSettings().setDefaultTextEncodingName("utf-8");
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.setBackgroundColor(Color.argb(0, 0, 0, 0));
		// 设置本地调用对象及其接口
		mWebView.addJavascriptInterface(new JavaScriptObject(this), "zfbPay");
		mWebView.setWebViewClient(new ZFBWebViewClient());
		mWebView.loadUrl("http://tui.vlcms.com/server.php/Alipay/index");

		if (!TextUtils.isEmpty(param)) {
			new Handler().postDelayed(new Runnable() {

				@Override
				public void run() {
					mWebView.loadUrl("javascript:submit('" + param + "')");
					MCLog.e("TEST", "-----------------");
				}
			}, 1000);
		} else {

		}

	}

	private void initView() {
		RelativeLayout topView = (RelativeLayout) findViewById(
				MCHInflaterUtils.getControl(MCPayWebView.this, "mch_webview_top"));
		if (isShowTopView) {
			topView.setVisibility(View.VISIBLE);
		}else{
			topView.setVisibility(View.GONE);
		}
		TextView txtTitle = (TextView) findViewById(
				MCHInflaterUtils.getControl(MCPayWebView.this, "tv_mch_header_title"));
		txtTitle.setText("支付宝付款");
		ImageView ivBack = (ImageView) findViewById(
				MCHInflaterUtils.getControl(MCPayWebView.this, "iv_mch_header_back"));
		ivBack.setVisibility(View.VISIBLE);
		ivBack.setOnClickListener(backClick);
	}

	OnClickListener backClick = new OnClickListener() {

		@Override
		public void onClick(View v) {
			finish();
		}
	};

	public class JavaScriptObject {
		Context mContxt;

		// @JavascriptInterface //sdk17版本以上加上注解
		public JavaScriptObject(Context mContxt) {
			this.mContxt = mContxt;
		}

		public void noticepayresult(String result) {
			// Toast.makeText(mContxt, name, Toast.LENGTH_LONG).show();
//			ZfbOrderInfoProcess.getInstance().setPayResult(result);
			finish();
		}
	}

	public class ZFBWebViewClient extends WebViewClient {

		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			view.loadUrl(url);
			return true;
		}

	}
}
